from mlh.utils.models import BaseModel
from django.db import models

# Create your models here.
from django.db.models import CASCADE

from django.db.models import PROTECT


from users.models import User


class Questions(BaseModel):
    """
    问题模型
    """
    author_name = models.ForeignKey(User, on_delete=CASCADE, related_name='questions', verbose_name='用户id')
    title = models.CharField(max_length=64, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    clicks = models.IntegerField(default=0, verbose_name='点赞量')
    pageviews = models.IntegerField(default=0, verbose_name='浏览量')
    labels = models.ManyToManyField(to='Label', db_table='q_l',related_name='question')
    answer_count = models.IntegerField(default=0, verbose_name='评论量')

    class Meta:
        db_table = 'tb_question'
        verbose_name = '问题'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Answer(BaseModel):
    """
    答案模型（评论）
    """
    user = models.ForeignKey(User, on_delete=CASCADE, related_name='answer', verbose_name='用户id')
    question = models.ForeignKey(Questions, on_delete=CASCADE, related_name='answer', verbose_name='问题id')
    content = models.CharField(max_length=256, verbose_name='评论内容')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True,
                               verbose_name='上级评论')
    clicks = models.IntegerField(default=0, verbose_name='点赞量')


    class Meta:
        db_table = 'tb_answer'
        verbose_name = '答案'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


class Label(BaseModel):
    """
    标签表
    """
    name = models.CharField(max_length=50, verbose_name='表签名')
    content = models.CharField(max_length=140, verbose_name='内容')
    attention = models.IntegerField(default=0, verbose_name='关注量')


    class Meta:
        db_table = 'tb_label'
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class QuestionClick(BaseModel):
    """
    多对多
    问题点赞表
    """
    question = models.ForeignKey(Questions, on_delete=CASCADE, verbose_name='问题id')
    user = models.ForeignKey(User, on_delete=CASCADE, verbose_name='用户id')
    is_like = models.BooleanField(verbose_name='点赞或踩')

    class Meta:
        db_table = 'u_q_clicks'

        verbose_name = '问题点赞表'
        verbose_name_plural = verbose_name


class UserAnswer(BaseModel):
    """
    多对多
    评论点赞表
    """
    user = models.ForeignKey(User, on_delete=CASCADE, verbose_name='用户id')
    answer = models.ForeignKey(Answer, on_delete=CASCADE, verbose_name='问题id')
    is_like = models.BooleanField(verbose_name='点赞或踩')

    class Meta:
        db_table = 'u_q_answer'
        verbose_name = '评论点赞表'
        verbose_name_plural = verbose_name


class UserLabel(BaseModel):
    """
    标签用户关联表
    """
    user = models.ForeignKey(User, on_delete=CASCADE, verbose_name='用户id')
    label = models.ForeignKey(Label, on_delete=CASCADE, verbose_name='标签id')

    class Meta:
        db_table = 'user_to_label'
        verbose_name = '标签关注表'
        verbose_name_plural = verbose_name